package cn.dxs.adt.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import cn.dxs.adt.R;
import cn.dxs.adt.adapter.NewsAdapter;
import cn.dxs.adt.http.news.NewsEntry;
import cn.dxs.adt.http.news.NewsLoader;
import cn.dxs.adt.utils.ClipboardUtil;
import cn.dxs.adt.utils.L;
import rx.functions.Action1;

/**
 * 新闻早班车
 */
public class NewsActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = NewsActivity.class.getSimpleName();

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    private EditText mEt_url;
    private Button mBtn_save;
    private RecyclerView mRc_news;
    private NewsLoader mNewsLoader;
    private NewsAdapter mAdp_news;
    private List<NewsEntry> mList_news;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news);
        initToolBar(toolbar, true, getTitle());
        init();
    }

    private void init() {
        initView();
        initData();
    }

    private void initView() {
        mEt_url = (EditText)findViewById(R.id.et_url);
        mBtn_save = (Button)findViewById(R.id.btn_save);
        mRc_news = (RecyclerView)findViewById(R.id.rc_news);
    }

    private void initData() {
        mNewsLoader = new NewsLoader();
        mBtn_save.setOnClickListener(this);
        getNewsList();
        // 线性布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRc_news.setLayoutManager(linearLayoutManager);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getClipboardData();
    }

    /**
     * 获取剪切板内容
     */
    private void getClipboardData() {
        this.getWindow().getDecorView().post(new Runnable() {
            @Override
            public void run() {
                String data = ClipboardUtil.paste();
                if (!TextUtils.isEmpty(data) && data.startsWith("https://mp.weixin.qq.com/")) {
                    mEt_url.setText(data);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                saveNews();
                break;
        }
    }

    /**
     * 給适配器设置数据
     */
    private void setAdpData() {
        if (mAdp_news == null) {
            mAdp_news = new NewsAdapter(mList_news);
            mRc_news.setAdapter(mAdp_news);
        } else {
            mAdp_news.notifyDataSetChanged(mList_news);
        }
    }

    private void getNewsList() {
        mNewsLoader.getNewsList().subscribe(new Action1<List<NewsEntry>>() {
            @Override
            public void call(List<NewsEntry> newsEntries) {
                L.i(TAG, "size: " + newsEntries.size());
                mList_news = newsEntries;
                setAdpData();
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }

    private void saveNews() {
        String url = mEt_url.getText().toString();
        if (url == null || url.trim().equals("")) {
            Toast.makeText(this, "地址不能为空", Toast.LENGTH_SHORT).show();
        }

        mNewsLoader.saveNews(url).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                L.i(TAG, "result: " + s);
                getNewsList();
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }
}